package com.cinc.ebc.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.mm.entity.MmMaterialPicking;
import com.cinc.ebc.mm.entity.MmMaterialPickingDetail;
import com.cinc.ebc.mm.entity.MmStockInOut;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.enums.MaterialPickingStatusEnum;
import com.cinc.ebc.mm.mapper.MmMaterialPickingDetailMapper;
import com.cinc.ebc.mm.mapper.MmMaterialPickingMapper;
import com.cinc.ebc.mm.model.MmMaterialPickingStockModel;
import com.cinc.ebc.mm.service.IMmMaterialPickingService;
import com.cinc.ebc.mm.service.IMmStockInOutService;
import com.cinc.ebc.mm.vo.MmMaterialPickingRequestPage;
import com.cinc.ebc.quote.product.entity.QuoteProduct;
import net.sf.saxon.functions.Empty;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 材料领用
 * @Author: jeecg-boot
 * @Date:   2023-04-16
 * @Version: V1.0
 */
@Service
public class MmMaterialPickingServiceImpl extends ServiceImpl<MmMaterialPickingMapper, MmMaterialPicking> implements IMmMaterialPickingService {

	@Autowired
	private MmMaterialPickingMapper mmMaterialPickingMapper;
	@Autowired
	private MmMaterialPickingDetailMapper mmMaterialPickingDetailMapper;

	@Autowired
	private IMmStockInOutService mmStockInOutService;


	@Override
	@Transactional
	public void saveMain(MmMaterialPicking mmMaterialPicking, List<MmMaterialPickingDetail> mmMaterialPickingDetailList) {
		mmMaterialPickingMapper.insert(mmMaterialPicking);
		if(mmMaterialPickingDetailList!=null && mmMaterialPickingDetailList.size()>0) {
			for(MmMaterialPickingDetail entity:mmMaterialPickingDetailList) {
				//外键设置
				entity.setMainId(mmMaterialPicking.getId());
				mmMaterialPickingDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(MmMaterialPicking mmMaterialPicking,List<MmMaterialPickingDetail> mmMaterialPickingDetailList) {
		MmMaterialPicking mmMaterialPickingOld = mmMaterialPickingMapper.selectById(mmMaterialPicking.getId());
		if(!mmMaterialPickingOld.getStatus().equals(MaterialPickingStatusEnum.NEW.getCode())){
			throw new RuntimeException("领料单已经开始处理，不能修改");
		}
		mmMaterialPickingMapper.updateById(mmMaterialPicking);

		//1.先删除子表数据
		mmMaterialPickingDetailMapper.deleteByMainId(mmMaterialPicking.getId());

		//2.子表数据重新插入
		if(mmMaterialPickingDetailList!=null && mmMaterialPickingDetailList.size()>0) {
			for(MmMaterialPickingDetail entity:mmMaterialPickingDetailList) {
				//外键设置
				entity.setMainId(mmMaterialPicking.getId());
				mmMaterialPickingDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		MmMaterialPicking mmMaterialPicking = mmMaterialPickingMapper.selectById(id);
		if(!mmMaterialPicking.getStatus().equals(MaterialPickingStatusEnum.NEW.getCode())){
			throw new RuntimeException("领料单已经开始处理，不能删除");
		}
		mmMaterialPickingDetailMapper.deleteByMainId(id);
		mmMaterialPickingMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			MmMaterialPicking mmMaterialPicking = mmMaterialPickingMapper.selectById(id);
			if(!mmMaterialPicking.getStatus().equals(MaterialPickingStatusEnum.NEW.getCode())){
				throw new RuntimeException("领料单已经开始处理，不能删除");
			}
			mmMaterialPickingDetailMapper.deleteByMainId(id.toString());
			mmMaterialPickingMapper.deleteById(id);
		}
	}

	@Transactional
	@Override
	public void handleOut(String id, List<MmMaterialPickingStockModel> pickingStockModelList) throws Exception {
		MmMaterialPicking materialPicking = mmMaterialPickingMapper.selectById(id);
		if(materialPicking == null){
			throw new RuntimeException("领料数据未找到!");
		}
		if(oConvertUtils.listIsEmpty(pickingStockModelList)){
			throw new RuntimeException("无可出库记录!");
		}
		int stockInOutLineNo = 0;
		List<MmStockInOutDetail> stockInOutDetailList = new ArrayList<>();
		for(MmMaterialPickingStockModel pickingStockModel : pickingStockModelList){
			// 更新领料明细
			mmMaterialPickingDetailMapper.updateOutQtyById(pickingStockModel.getDetailId(), pickingStockModel.getQty());
			MmMaterialPickingDetail pickingDetail = mmMaterialPickingDetailMapper.selectById(pickingStockModel.getDetailId());
			if(pickingDetail.getOutQty().compareTo(pickingDetail.getQty()) == 1){
				throw new RuntimeException("领用总量不能超出制单数量!");
			}
			// 出库明细
			MmStockInOutDetail stockInOutDetail = new MmStockInOutDetail();
			stockInOutLineNo += 10;
			stockInOutDetail.setLineNo(stockInOutLineNo);
			stockInOutDetail.setMaterialId(pickingStockModel.getMaterialId());
			stockInOutDetail.setMaterialNo(pickingStockModel.getMaterialNo());
			stockInOutDetail.setMaterialDesc(pickingStockModel.getMaterialDesc());
			stockInOutDetail.setUnit(pickingStockModel.getUnit());
			stockInOutDetail.setQty(pickingStockModel.getQty());
			stockInOutDetail.setUnitPrice(pickingStockModel.getUnitPrice());
			BigDecimal sumPrice = pickingStockModel.getUnitPrice().multiply(pickingStockModel.getQty()).setScale(2, BigDecimal.ROUND_HALF_UP);
			stockInOutDetail.setSumPrice(sumPrice);
			stockInOutDetail.setSourceId(pickingStockModel.getDetailId());
			stockInOutDetail.setSourceNo(materialPicking.getBillNo());
			stockInOutDetail.setWarehouseId(pickingStockModel.getWarehouseId());
			stockInOutDetail.setStorageLocationId(pickingStockModel.getStorageLocationId());
			stockInOutDetail.setBatchNo(pickingStockModel.getBatchNo());
			stockInOutDetail.setProjectId(pickingStockModel.getProjectId());

			stockInOutDetailList.add(stockInOutDetail);
		}
		// 生成出库单
		MmStockInOut stockInOut = new MmStockInOut();
		stockInOut.setBillDate(new Date());
		stockInOut.setProjectId(materialPicking.getProjectId());
		mmStockInOutService.saveMainMaterialPicking(stockInOut, stockInOutDetailList, materialPicking);

		// 更新领料主表
		mmMaterialPickingMapper.autoUpdateStatusById(id);

	}

	@Override
	public IPage<MmMaterialPicking> pickingPage(Page<MmMaterialPicking> page, MmMaterialPickingRequestPage mmMaterialPickingRequest) {
		String sql = QueryGenerator.installAuthJdbc(MmMaterialPicking.class);
		return page.setRecords(baseMapper.selectPageList(page, mmMaterialPickingRequest, sql));
	}

}
